package xyz.scootaloo.mono.base.util

import cn.hutool.core.util.RandomUtil
import cn.hutool.crypto.SecureUtil

/**
 * 加密工具
 *
 * @author flutterdash@qq.com
 * @since 2021/7/29 16:19
 */
object EncryptUtil {

    fun md5(value: String, salt: String): String {
        return SecureUtil.md5("$value$salt")
    }

    fun match(encrypted: String, value: String, salt: String): Boolean {
        return encrypted == md5(value, salt)
    }

    fun generateSalt(len: Int = 8): String {
        return RandomUtil.randomString(len)
    }

    class PasswordEncoder(
        val password: String,
        var encoded: String = "",
        var salt: String = ""
    ) {
        init {
            salt = generateSalt()
            encoded = md5(password, salt)
        }
    }
}
